﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;


namespace baseClass.Controls
{
    public class gridUser : common.controls.baseDataGridView 
    {
        public enum ColumnNames
        {
            account,lastName,firstName, type, status,isGroup
        };

        public gridUser()
        {
        }

        public override void LockEdit(bool lockStat)
        {
            base.LockEdit(true); 
        }
        public override void Init()
        {
            base.Init();
            data.baseDS.geUserDataTable dummyTbl = DataAccess.DataCache.myBaseDS.geUser;

            // =============================
            // detailGrid
            // =============================

            // account
            common.controls.DataGridViewTextBoxColumnExt account = new common.controls.DataGridViewTextBoxColumnExt();
            account.Uppercase = true;
            account.DataPropertyName = dummyTbl.loginColumn.ColumnName;
            account.HeaderText = "Tài khoản";
            account.Name = ColumnNames.account.ToString();
            account.Width = 120;

            // lastName
            common.controls.DataGridViewTextBoxColumnExt lastName = new common.controls.DataGridViewTextBoxColumnExt();
            lastName.DataPropertyName = dummyTbl.lastNameColumn.ColumnName;
            lastName.HeaderText = "Họ và lót";
            lastName.Name = ColumnNames.lastName.ToString();
            lastName.Width = 130;

            // firstName
            common.controls.DataGridViewTextBoxColumnExt firstName = new common.controls.DataGridViewTextBoxColumnExt();
            firstName.DataPropertyName = dummyTbl.lastNameColumn.ColumnName;
            firstName.HeaderText = "Tên";
            firstName.Name = ColumnNames.firstName.ToString();
            firstName.Width = 120;

            // type
            // 
            DataAccess.DataCache.LoadUserType(false);
            BindingSource userTypeSrc = new BindingSource();
            userTypeSrc.DataSource = DataAccess.DataCache.myBaseDS.userType;

            DataGridViewComboBoxColumn type = new DataGridViewComboBoxColumn();
            type.DataSource = userTypeSrc;
            type.DataPropertyName = dummyTbl.loginColumn.ColumnName;
            type.DisplayMember = DataAccess.DataCache.myBaseDS.userType.descriptionColumn.ColumnName;
            type.ValueMember = DataAccess.DataCache.myBaseDS.userType.codeColumn.ColumnName;
            type.HeaderText = "Loại";
            type.Name = ColumnNames.type.ToString();
            type.ReadOnly = true;

            type.Width = 120;
            type.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            type.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // status
            DataGridViewImageColumn status = new DataGridViewImageColumn();
            status.DataPropertyName = dummyTbl.statusColumn.ColumnName;
            status.Name = ColumnNames.status.ToString();
            status.Width = 25;

            // isGroup
            DataGridViewImageColumn isGroup = new DataGridViewImageColumn();
            isGroup.DataPropertyName = dummyTbl.isGroupColumn.ColumnName;
            isGroup.Name = ColumnNames.isGroup.ToString();
            isGroup.Width = 25;

            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {status,isGroup,account, lastName,type});

            this.myAutoFitColumn = ColumnNames.lastName.ToString();
            this.AutoFit();
        }

        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
        {
            base.OnCellFormatting(e);
            try
            {
                if (e == null || this.IsValidCell(e.RowIndex, e.ColumnIndex) == false) return;
                if (this.Columns[e.ColumnIndex].Name == ColumnNames.status.ToString())
                {
                    if (e.Value != null)
                        e.Value = common.SysLibs.GetStatusImage((byte)e.Value);
                }
                if (this.Columns[e.ColumnIndex].Name == ColumnNames.isGroup.ToString())
                {
                    if (e.Value == null) return;
                    e.Value = ((bool)e.Value ? baseClass.Properties.Resources.usergroup : baseClass.Properties.Resources.user);
                }
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }
    }
}
